<?php namespace Common\Model; 

use Hdphp\Model\Model;
use Common\Model\Goods;

//类型管理(实现增删改查)
class GoodsList extends Model{
	//绑定wsshop_goods表
	protected $table = 'goods_list';
	
	//自动完成
	protected $auto = [
		['number','time','function',3,1],
	];
	
	//添加货品
	public function store(){
		
		if(isset($_POST['combineID'][1])){
			//组合属性id
			$_POST['combineID'] = $_POST['combineID'][0].','.$_POST['combineID'][1];
		}else{
			$_POST['combineID'] = $_POST['combineID'][0];
		}
		//不传入数据返回false
		if(!$this->create()) return false;
		//传入数据返回true
		$this->add();
		
		//添加总库存
		//第一步：获取货品列表中的对应商品的全部库存，如gid=1
		$gid = Q('get.gid');
		$goodsListModel = new GoodsList();
		$stock = $goodsListModel->where("wsshop_goods_gid={$gid}")->sum('inventory');
		//第二步：添加商品表的总库存
		$goodsModel = new Goods();
		$goodsModel->where("gid={$gid}")->update(['stock'=>$stock]);
		
		return true;
	}
	
	//修改货品
	public function edit(){
		//组合属性id
		$_POST['combineID'] = $_POST['combineID'][0].','.$_POST['combineID'][1];
		die;
		//不传入数据返回false
		if(!$this->create()) return false;
		//传入数据就修改并返回true
		$this->save();
		
		//添加总库存
		//第一步：获取货品列表中的对应商品的全部库存，如gid=1
		$gid = Q('get.gid');
		$goodsListModel = new GoodsList();
		$stock = $goodsListModel->where("wsshop_goods_gid={$gid}")->sum('inventory');
		//第二步：添加商品表的总库存
		$goodsModel = new Goods();
		$goodsModel->where("gid={$gid}")->update(['stock'=>$stock]);
		
		return true;
		
		
		
	}
}
